/**
 * Created by python on 19-2-28.
 */
let hdd = new Vue({
    el: "#hdd",
    data: {
        // 页面中需要使用到的数据，键值对
        user_id: sessionStorage.user_id || localStorage.user_id,
        token: sessionStorage.token || localStorage.token,
        host: HOST,
        columns: null,
        headlines: null,
        column: 0,
        headlines_hot: '',
        headlines_content: null,
        page:1,
        page_size:2,
        count:0,
        activities:null,
        discussions:null,
        discussion_list:null,
        activ: null,

    },
    computed: {
        total_page: function(){  // 总页数
            return Math.ceil(this.count/this.page_size);
        },
        next: function(){  // 下一页
            if (this.page >= this.total_page) {
                return 0;
            } else {
                return this.page + 1;
            }
        },
        previous: function(){  // 上一页
            if (this.page <= 0 ) {
                return 0;
            } else {
                return this.page - 1;
            }
        },
        page_nums: function(){  // 页码
            // 分页页数显示计算
            // 1.如果总页数<=5
            // 2.如果当前页是前3页
            // 3.如果当前页是后3页,
            // 4.既不是前3页，也不是后3页
            var nums = [];
            if (this.total_page <= 5) {
                for (var i=1; i<=this.total_page; i++){
                    nums.push(i);
                }
            } else if (this.page <= 3) {
                nums = [1, 2, 3, 4, 5];
            } else if (this.total_page - this.page <= 2) {
                for (var i=this.total_page; i>this.total_page-5; i--) {
                    nums.push(i);
                }
            } else {
                for (var i=this.page-2; i<this.page+3; i++){
                    nums.push(i);
                }
            }
            return nums;
        }
    },
    mounted: function () {

        this.get_headline(this.column);
        this.headlines_longin();
        this.get_click_hotheadline();
        this.headlines_activity();
        this.headlines_discussion();
        this.headlines_activity_act();

    },
    methods: {
        // 需要用到的函数，键值对 ，键是名称，值是匿名函数
        headlines_longin: function () {
            axios.get(this.host + '/index/', {
                // headers: {
                //     'Authorization': 'JWT ' + this.token
                //     },
                responseType: 'json',
                withCredentials: true,
            })
                .then(response => {
                    console.log(response.data);
                    this.columns = response.data;
                });
            // .catch(error =>{
            //
            // })
        },
        get_headline: function (column_id) {
            this.column = column_id;

            axios.get(this.host + '/index/' + column_id + '/headlines/', {
                params:{
                    page: this.page,
                    page_size: this.page_size,
                },
                // headers: {
                //     'Authorization': 'JWT ' + this.token
                //     },
                responseType: 'json',
                withCredentials: true
            })
                .then(response => {
                    this.headlines = response.data.results;
                    this.count = response.data.count
                });
        },
        // 不感兴趣功能未实现
        // no_like: function (headline_id) {
        //     console.log(headline_id)
        // },
        get_click_hotheadline:function () {
            axios.get(this.host + '/gethotheadline/',{
                params:{
                    page: this.page,
                    page_size: this.page_size,
                },
                responseType: 'json',
                withCredentials: true
            }).then(response => {
                this.headlines_hot = response.data.results;
                this.count = response.data.count;

            })
        },
        on_page: function(num){
            if (num != this.page){
                this.page = num;
                this.get_click_hotheadline();
                this.get_headline(this.column);
            }
        },
        // 首页活动
        headlines_activity: function () {
            axios.get(this.host + '/activity/', {
                // headers: {
                //     'Authorization': 'JWT ' + this.token
                //     },
                responseType: 'json',
                withCredentials: true,
            })
                .then(response => {
                    console.log(response.data);
                    this.activities = response.data;
                });
            // .catch(error =>{
            //
            // })
        },
        // 活动日历
        headlines_activity_act: function (pk) {
            axios.get(this.host + '/activity/' + pk, {
                // headers: {
                //     'Authorization': 'JWT ' + this.token
                //     },
                responseType: 'json',
                withCredentials: true,
            })
                .then(response => {
                    this.activ = response.data;
                    console.log(response.data);
                });
            // .catch(error =>{
            //
            // })
        },
        // 首页热门问答
        headlines_discussion: function () {
            axios.get(this.host + '/discussion/', {
                // headers: {
                //     'Authorization': 'JWT ' + this.token
                //     },
                responseType: 'json',
                withCredentials: true,
            })
                .then(response => {
                    this.discussions = response.data;
                    this.discussion_list = [];
                    this.discussion_list[0]=  this.discussions[0];
                    this.discussion_list[1]=  this.discussions[1];
                    // this.discussion_list[2]=  this.discussions[2];
                    // this.discussion_list[3]=  this.discussions[3];
                    console.log(response.data);
                });
            // .catch(error =>{
            //
            // })
        },
        mouse_ale:function () {
            layer.msg('都说了别点嘛～ 嘤嘤嘤');
        }
    },

})